package com.yangrong.newsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangrong.common.utils.PageUtils;
import com.yangrong.common.utils.Query;
import com.yangrong.newsservice.dao.NewsDao;
import com.yangrong.newsservice.entity.NewsEntity;
import com.yangrong.newsservice.service.NewsService;
import com.yangrong.newsservice.utils.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("newsService")
public class NewsServiceImpl extends ServiceImpl<NewsDao, NewsEntity> implements NewsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NewsEntity> page = this.page(
                new Query<NewsEntity>().getPage(params),
                new QueryWrapper<NewsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<String> getListType() {
        List<String> returnList = new ArrayList<>();
        returnList.add("推荐");
        baseMapper.getListType().forEach(item -> {
            returnList.add(item);
        });
        return returnList;
    }

    @Override
    public List<NewsEntity> getNewsListByType(Map<String, Object> params) {
        String type = (String) params.get("type");
        if (StringUtils.isNotBlank(type) && (type.equals("推荐")) || StringUtils.isBlank(type)) {
            return baseMapper.getNewsList();
        } else {
            return baseMapper.getNewsListByType(type);
        }

    }

    @Override
    public Integer getAllNewsSize(Map<String, Object> params) {
        String type = (String) params.get("type");
        if(StringUtils.isNotBlank(type)) {
            return baseMapper.getAllNewsSizeWithType(type);
        } else {
            return baseMapper.getAllNewsSizeNoType();
        }
    }

    @Override
    public List<NewsEntity> getMore(Map<String, Object> params) {
        String type = (String) params.get("type");
        String start = (String) params.get("start");
        Integer getSize = 5;

        if(StringUtils.isNotBlank(type)) {
            return baseMapper.getMoreWithType(type, Integer.parseInt(start), getSize);
        } else {
            return baseMapper.getMoreNoType(Integer.parseInt(start), getSize);
        }
    }

    @Override
    public String getContent(String title) {
        String content = FileUtils.readFile(title);
        return content;
    }

}